Optional values (Maybe)

The type Maybe a can be used in situations where some value can not be necessarily computed. Its values are either Nothing or Just v where the type of v is a.

data Maybe a (Builtin)

Represents an optional value.

Nothing :: Maybe b (Builtin)
Just :: b -> Maybe b (Builtin)
fromJust :: Maybe a -> a (Prelude)

Given Just x this function returns x. If the parameter is Nothing, the function raises an exception.

fromMaybe :: a -> Maybe a -> a (Prelude)

fromMaybe def v returns def if v=Nothing and x if v=Just x.

execJust :: Maybe a -> (a -> <c> b) -> <c> () (Prelude)

execJust v f executes the function f with parameter value x, if v=Just x. If v=Nothing, the function does nothing.

filterJust :: [Maybe a] -> [a] (Prelude)

Takes those elements of the input list that match (Just x) and adds the contents to the resulting list. For example,

filterJust [Just 1, Nothing, Just 5] = [1, 5]
orElse :: Maybe a -> <b> a -> <b> a (Prelude)

Provides a default value if the first parameter is Nothing. The default value is evaluated only if needed. The function can be used as an operator and is right associative so that the following is possible:

tryWithTheFirstMethod
    `orElse` tryWithTheSecondMethod
    `orElse` fail "Didn't succeed."
elemMaybe :: a -> Maybe a -> Boolean (Prelude)

elemMaybe v1 (Just v2) returns true if v1 == v2. elemMaybe v1 Nothing is always false.